package com.xiongda.controller;

import com.xiongda.common.Const;
import com.xiongda.common.ServerResponse;
import com.xiongda.dto.SafetyProblemDto;
import com.xiongda.dto.UserDto;
import com.xiongda.pojo.User;
import com.xiongda.service.SafetyProblemService;
import com.xiongda.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * 用户模块
 *
 * @author 缪琼
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;
    @Autowired
    private SafetyProblemService safetyProblemService;

    /**
     * 用户登录接口
     * @param userDto
     * @param session
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ServerResponse<User> login(@RequestBody UserDto userDto, HttpSession session) {
        ServerResponse<User> response = userService.login(userDto);
        if (response.isSuccess())
            session.setAttribute(Const.CURRENT_USER, response.getData());
        return response;
    }

    /**
     * 用户注册
     * @param userDto
     * @param session
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ServerResponse<User> register(@RequestBody UserDto userDto, HttpSession session) {
        return userService.register(userDto);
    }

    /**
     * 用户激活
     * @param code
     * @return
     */
    @RequestMapping(value = "/user_code", method = RequestMethod.GET)
    public String userCode(String code, HttpSession session) {
        ServerResponse response = userService.updateUserByCode(code);
        session.setAttribute("message", response.getMsg());
        return "home/active_code";
    }

    /**
     * 忘记密码-用户名
     * @param userDto
     * @param session
     * @return
     */
    @RequestMapping(value = "/forget_password", method = RequestMethod.POST)
    public ServerResponse forgetPassword(@RequestBody UserDto userDto, HttpSession session) {
        return userService.checkUserName(userDto);
    }

    /**
     * 安全问题校验
     * @param safetyProblemDto
     * @param session
     * @return
     */
    @RequestMapping(value = "/check_question", method = RequestMethod.POST)
    public ServerResponse checkQuestion(@RequestBody SafetyProblemDto safetyProblemDto, HttpSession session) {
        ServerResponse response = userService.getUserByUserName(safetyProblemDto.getUserName());
        if (response.getStatus() == 1)
            return response;
        safetyProblemDto.setUserId(((User) response.getData()).getId());
        return safetyProblemService.checkQuestion(safetyProblemDto);
    }

    /**
     * 修改密码
     * @param userDto
     * @param session
     * @return
     */
    @RequestMapping(value = "/edit_password", method = RequestMethod.POST)
    public ServerResponse editPassword(@RequestBody UserDto userDto, HttpSession session){
        ServerResponse response = userService.getUserByUserName(userDto.getUserName());
        if (response.getStatus() == 1)
            return response;
        userDto.setId(((User)response.getData()).getId());
        return userService.editPassword(userDto);
    }
}